
package Control;

import Database.ConexaoDB;
import Model.Cargo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class Control_Cargo {
    
    Connection con = new ConexaoDB().conectar();
    PreparedStatement pstm;
    ResultSet rs;
    
    String cadastraCargo = "INSERT INTO CARGO(CARGO_ID, CARGO_DESCRICAO) VALUES(0,?)";
    
    String alteraCargo = "UPDATE CARGO SET CARGO_DESCRICAO=? WHERE CARGO_ID=?";
     
    String excluiCargo = "DELETE FROM CARGO WHERE CARGO_ID=?";
    
    String consultaCargoCodigo = "SELECT * FROM CARGO WHERE CARGO_ID LIKE ?";
    
    String consultaCargoNome = "SELECT * FROM CARGO WHERE CARGO_DESCRICAO LIKE ?";
    
    String consultaCargoTodosNome = "SELECT * FROM CARGO ORDER BY CARGO_DESCRICAO";
    
    String consultaCargoTodosCodigo = "SELECT * FROM CARGO ORDER BY CARGO_ID";
    
      
    ConexaoDB BD = new ConexaoDB();
    
    
    public Control_Cargo(){ }                                
    
    public void cadastrarCargo(Cargo cargo){
         try{
            pstm = BD.conectar().prepareStatement(cadastraCargo);
            pstm.setString(1, cargo.getCargo_descricao());
            pstm.executeUpdate();
            BD.desconectar(con);
         }
         catch(Exception e){
               e.printStackTrace();   
            }
    }
    
    public void alterarCargo(Cargo cargo){
         try{
            pstm = BD.conectar().prepareStatement(alteraCargo);
            pstm.setString(1, cargo.getCargo_descricao());
            pstm.setInt(2, cargo.getCargo_id());
            pstm.executeUpdate();
            BD.desconectar(con);
         }
         catch(Exception e){
               e.printStackTrace();   
            }
    }
    
    public void excluirCargo(Cargo cargo){
         try{
            pstm = BD.conectar().prepareStatement(excluiCargo);
            pstm.setInt(1, cargo.getCargo_id());
            pstm.executeUpdate();
            BD.desconectar(con);
         }
         catch(Exception e){
               e.printStackTrace();   
            }
    }
    
    public java.util.List<Cargo> listarCargoCodigo(String codigo){
        java.util.List<Cargo> cargo = new ArrayList();
        try{
            pstm = BD.conectar().prepareStatement(consultaCargoCodigo);
            pstm.setString(1, codigo);
            rs = pstm.executeQuery();
            
            while (rs.next()){
                Cargo car = new Cargo();
                car.setCargo_id(rs.getInt("cargo_id"));
                car.setCargo_descricao(rs.getString("cargo_descricao"));
                cargo.add(car);
            }
            BD.desconectar(con);
        } catch(Exception e){
            e.printStackTrace();
        }
        return cargo;
    }
    
    public java.util.List<Cargo> listarCargoNome(String nome){
        java.util.List<Cargo> cargo = new ArrayList();
        try{
            pstm = BD.conectar().prepareStatement(consultaCargoNome);
            pstm.setString(1, nome);
            rs = pstm.executeQuery();
            
            while (rs.next()){
                Cargo car = new Cargo();
                car.setCargo_id(rs.getInt("cargo_id"));
                car.setCargo_descricao(rs.getString("cargo_descricao"));
                cargo.add(car);
            }
            BD.desconectar(con);
        } catch(Exception e){
            e.printStackTrace();
        }
        return cargo;
    }
    
    public java.util.List<Cargo> listarCargoTodosNome(){
        java.util.List<Cargo> cargo = new ArrayList();
        try{
            pstm = BD.conectar().prepareStatement(consultaCargoTodosNome);
            rs = pstm.executeQuery();
            
            while (rs.next()){
                Cargo car = new Cargo();
                car.setCargo_id(rs.getInt("cargo_id"));
                car.setCargo_descricao(rs.getString("cargo_descricao"));
                cargo.add(car);
            }
            BD.desconectar(con);
        } catch(Exception e){
            e.printStackTrace();
        }
        return cargo;
    }
    
    public java.util.List<Cargo> listarCargoTodosCodigo(){
        java.util.List<Cargo> cargo = new ArrayList();
        try{
            pstm = BD.conectar().prepareStatement(consultaCargoTodosCodigo);
            rs = pstm.executeQuery();
            
            while (rs.next()){
                Cargo car = new Cargo();
                car.setCargo_id(rs.getInt("cargo_id"));
                car.setCargo_descricao(rs.getString("cargo_descricao"));
                cargo.add(car);
            }
            BD.desconectar(con);
        } catch(Exception e){
            e.printStackTrace();
        }
        return cargo;
    }
    
}
